package cn.doitedu.vueapi.service.tg;

import cn.doitedu.vueapi.dao.tg.AdGenRecMapper;
import cn.doitedu.vueapi.dao.tg.AdResourceMapper;
import cn.doitedu.vueapi.pojo.tg.AdGenRec;
import cn.doitedu.vueapi.pojo.tg.AdResource;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class AdResourceServiceImpl {

    @Autowired
    AdResourceMapper adResourceMapper;

    @Autowired
    AdGenRecMapper adGenRecMapper;

    public List<AdResource> getAdList(AdResource ad){

        List<AdResource> ads = adResourceMapper.selectByAdResource(ad);

        return ads;

    }


    /**
     * 生成二维码
     * @param ad
     * @param uid
     * @return
     */
    public String genChannelAd(AdResource ad,int uid){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String dt = sdf.format(new Date());

        // 判断之前是否已经生成过,存在过则从记录表中查询返回
        AdGenRec adGenRec = new AdGenRec();
        adGenRec.setUid(uid);
        adGenRec.setAdid(ad.getId());
        List<AdGenRec> recs = adGenRecMapper.selectByAdGenRec(adGenRec);
        if(recs !=null && recs.size()>0) return recs.get(0).getPosImg();


        // 判断广告是否存在
        List<AdResource> ads = adResourceMapper.selectByAdResource(ad);

        // 构造默认返回结果，成品广告的图片路径
        String channelAdUrl = "error";

        // 如果广告id存在
        if(ads.size() == 1){
            AdResource adr = ads.get(0);
            int adId = adr.getId();
            String oriad = adr.getMbImg();
            Integer loc_x = adr.getEwmLeft();
            Integer loc_y = adr.getEwmTop();
            Integer ewm_w = adr.getEwmWidth();
            Integer ewm_h = adr.getEwmHeight();

            String jumpUrl = adr.getJumpUrl();

            // 拼接跟踪码
            String trace = uid+":"+adId;

            // 机密跟踪码
            byte[] encTrace = Base64.encodeBase64(trace.getBytes());

            // 生成活码的跳转地址
            String url = jumpUrl+"?trace="+new String(encTrace);

            // 调用python - rest 生成 活码海报
            try {
                CloseableHttpClient client = HttpClientBuilder.create().build();

                /***
                 *         oriad = "/www/wwwroot/www.doitedu.cn/haibao/origin/yy.jpg"
                 *         channel_out = "/www/wwwroot/www.doitedu.cn/haibao/channels/yy_channel.jpg"
                 *         logo = "/www/wwwroot/www.doitedu.cn/haibao/logo/log-touming.png"
                 *         url = "http://www.doitedu.cn/ewm.html?code=wei34sdkgjk"
                 *         ewm_w = 80
                 *         ewm_h = 80
                 *         loc_x = 538
                 *         loc_y = 910
                 */
                StringBuilder sb = new StringBuilder();
                sb.append("http://localhost:8080/1u24732j235ab9ek?").append("&");
                sb.append("oriad="+oriad).append("&");
                String pos_img_name = adId+"_"+uid+"_"+dt+".jpg";
                sb.append("channel_out="+pos_img_name).append("&");
                sb.append("url="+url).append("&");
                sb.append("ewm_w="+ewm_w).append("&");
                sb.append("ewm_h="+ewm_h).append("&");
                sb.append("loc_x="+loc_x).append("&");
                sb.append("loc_y="+loc_y);

                HttpGet get = new HttpGet(sb.toString());

                HttpEntity entity = client.execute(get).getEntity();
                String res = EntityUtils.toString(entity);
                if ("0".equals(res)) {
                    channelAdUrl = pos_img_name;

                    // 向生成记录表中插入一条生成记录
                    adGenRec.setPosImg(pos_img_name);
                    adGenRec.setCreateTime(new Date());
                    adGenRecMapper.insertAdGenRec(adGenRec);

                    return channelAdUrl;
                }
            }catch (Exception e){

            }

        }

        return channelAdUrl;
    }




}
